package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.*;
import his.persistence.*;
import his.service.DrugsService;
import his.vo.Condition;
import his.vo.PrescriptionVO;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class DrugsServiceImpl implements DrugsService {

    @Autowired
    DrugsMapper drugsMapper;

    @Autowired
    PrescriptionMapper prescriptionMapper;

    @Autowired
    PrescriptionDetailedMapper prescriptionDetailedMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MedicalRecordMapper medicalRecordMapper;

    @Override
    public CommonResponse<List<PrescriptionVO>> get_drugs_bycasenumber(String casenumber) {

        List<PrescriptionVO> prescriptionVOList=new ArrayList<>();
        QueryWrapper<MedicalRecord> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("CaseNumber",casenumber);
        List<MedicalRecord> medicalRecordList=medicalRecordMapper.selectList(queryWrapper1);
        for(int i=0;i<medicalRecordList.size();i++){
            prescriptionVOList.addAll(this.check_drug(medicalRecordList.get(i).getID()).getData());

        }
        return CommonResponse.createForSuccess(prescriptionVOList);
    }

    @Override
    public CommonResponse delete_drugs(String drug_flag) {

        QueryWrapper<Drugs> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DrugsName",drug_flag);
        List<Drugs> drugsList1=drugsMapper.selectList(queryWrapper1);
        if(drugsList1.size()>0){
            QueryWrapper<Drugs> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DrugsName",drug_flag);
            int result=drugsMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess("删除成功");
        }else{
            QueryWrapper<Drugs> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("MnemonicCode",drug_flag);
            List<Drugs> drugsList2=drugsMapper.selectList(queryWrapper3);
            if(drugsList2.size()>0){
                QueryWrapper<Drugs> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("MnemonicCode",drug_flag);
                int result=drugsMapper.delete(queryWrapper4);
                return CommonResponse.createForSuccess("删除成功");
            }else{
                return CommonResponse.createForError("删除失败");
            }

        }

    }

    @Override
    public CommonResponse modify_drugs(String DrugsCode, String DrugsName, String DrugsFormat, String DrugsUnit, String Manufacturer, int DrugsDosageID, int DrugsTypeID, BigDecimal DrugsPrice, String MnemonicCode) {

        QueryWrapper<Drugs> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DrugsName",DrugsName);
        List<Drugs> drugsList1=drugsMapper.selectList(queryWrapper1);
        if(drugsList1.size()>0){
            QueryWrapper<Drugs> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DrugsName",DrugsName);
            Drugs drugs=new Drugs();
            drugs.setDrugsCode(DrugsCode);
            drugs.setDrugsFormat(DrugsFormat);
            drugs.setDrugsUnit(DrugsUnit);
            drugs.setManufacturer(Manufacturer);
            drugs.setDrugsDosageID(DrugsDosageID);
            drugs.setDrugsTypeID(DrugsTypeID);
            drugs.setDrugsPrice(DrugsPrice);
            drugs.setMnemonicCode(MnemonicCode);
            Date date1=new Date();
            drugs.setLastUpdateDate(date1);
            drugs.setDelMark(1);
            int result=drugsMapper.update(drugs,queryWrapper2);
            return CommonResponse.createForSuccess("修改成功");
        }else{
            return CommonResponse.createForError("修改失败");
        }

    }

    @Override
    public CommonResponse add_drugs(String DrugsCode, String DrugsName, String DrugsFormat, String DrugsUnit, String Manufacturer, int DrugsDosageID, int DrugsTypeID, BigDecimal DrugsPrice, String MnemonicCode) {

        QueryWrapper<Drugs> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DrugsName",DrugsName);
        List<Drugs> drugsList1=drugsMapper.selectList(queryWrapper1);
        if(drugsList1.size()>0){
            return CommonResponse.createForError("新增失败");
        }else{
            Drugs drugs=new Drugs();
            drugs.setDrugsCode(DrugsCode);
            drugs.setDrugsName(DrugsName);
            drugs.setDrugsFormat(DrugsFormat);
            drugs.setDrugsUnit(DrugsUnit);
            drugs.setManufacturer(Manufacturer);
            drugs.setDrugsDosageID(DrugsDosageID);
            drugs.setDrugsTypeID(DrugsTypeID);
            drugs.setDrugsPrice(DrugsPrice);
            drugs.setMnemonicCode(MnemonicCode);
            Date date1=new Date();
            drugs.setCreationDate(date1);
            drugs.setDelMark(1);
            drugsMapper.insert(drugs);
            QueryWrapper<Drugs> queryWrapper2=new QueryWrapper<>();
            return CommonResponse.createForSuccess("新增成功");
        }

    }

    @Override
    public CommonResponse<List<Drugs>> getall_drugs_bycodename(String keywords) {

        QueryWrapper<Drugs> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MnemonicCode",keywords);
        List<Drugs> drugsList1=drugsMapper.selectList(queryWrapper1);
        if(drugsList1.size()>0){
            return CommonResponse.createForSuccess(drugsList1);
        }else{
            QueryWrapper<Drugs> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DrugsName",keywords);
            List<Drugs> drugsList2=drugsMapper.selectList(queryWrapper2);
            if(drugsList2.size()>0){
                return CommonResponse.createForSuccess(drugsList2);
            }else{
                return CommonResponse.createForError("查询失败");
            }
        }

    }

    @Override
    public CommonResponse<List<Drugs>> getall_drugs() {

        QueryWrapper<Drugs> queryWrapper1=new QueryWrapper<>();
        List<Drugs> drugsList=drugsMapper.selectList(queryWrapper1);
        if(drugsList.size()>0){
            return CommonResponse.createForSuccess(drugsList);
        }else{
            return CommonResponse.createForError("查询失败");
        }

    }

    @Override
    public CommonResponse return_drug(String drugsName, String prescriptionTime) {
        QueryWrapper<Drugs> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("DrugsName",drugsName);
        Drugs drugs=drugsMapper.selectOne(queryWrapper);

        QueryWrapper<Prescription> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("PrescriptionTime",prescriptionTime);
        Prescription prescription=prescriptionMapper.selectOne(queryWrapper1);
        QueryWrapper<PrescriptionDetailed> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("PrescriptionID",prescription.getID());
        queryWrapper2.eq("DrugsID",drugs.getID());
        PrescriptionDetailed prescriptionDetailed=new PrescriptionDetailed();
        prescriptionDetailed.setState(5);
        prescriptionDetailed.setPrescriptionID(prescription.getID());
        prescriptionDetailed.setDrugsID(drugs.getID());
        prescriptionDetailedMapper.update(prescriptionDetailed,queryWrapper2);
        return CommonResponse.createForSuccess("退药成功");

    }

    @Override
    public CommonResponse deliver_drug(String drugsName, String prescriptionTime) {
        QueryWrapper<Drugs> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("DrugsName",drugsName);
        Drugs drugs=drugsMapper.selectOne(queryWrapper);

        QueryWrapper<Prescription> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("PrescriptionTime",prescriptionTime);
        Prescription prescription=prescriptionMapper.selectOne(queryWrapper1);
        QueryWrapper<PrescriptionDetailed> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("PrescriptionID",prescription.getID());
        queryWrapper2.eq("DrugsID",drugs.getID());
        PrescriptionDetailed prescriptionDetailed=new PrescriptionDetailed();
        prescriptionDetailed.setState(4);
        prescriptionDetailed.setPrescriptionID(prescription.getID());
        prescriptionDetailed.setDrugsID(drugs.getID());
        prescriptionDetailedMapper.update(prescriptionDetailed,queryWrapper2);
        return CommonResponse.createForSuccess("发药成功");

    }

    @Override
    public CommonResponse<List<PrescriptionVO>> check_drug( int medical_id) {

        List<PrescriptionVO> prescriptionVOList=new ArrayList<>();
        QueryWrapper<Prescription> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MedicalID",medical_id);
        List<Prescription> prescriptionList1=prescriptionMapper.selectList(queryWrapper1);
        for(int i=0;i<prescriptionList1.size();i++){
            Prescription prescription=prescriptionList1.get(i);
            QueryWrapper<PrescriptionDetailed> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("PrescriptionID",prescriptionList1.get(i).getID());
            List<PrescriptionDetailed> prescriptionDetailedList=prescriptionDetailedMapper.selectList(queryWrapper2);
            for(int j=0;j<prescriptionDetailedList.size();j++){
                PrescriptionDetailed prescriptionDetailed=prescriptionDetailedList.get(j);
                PrescriptionVO prescriptionVO=new PrescriptionVO();
                prescriptionVO.setDrugs_id(prescriptionDetailed.getDrugsID());
                prescriptionVO.setNum(prescriptionDetailed.getAmount());
                prescriptionVO.setState(prescriptionDetailed.getState());
                prescriptionVO.setPrescriptionName(prescription.getPrescriptionName());
                prescriptionVO.setPrescriptionTime(prescription.getPrescriptionTime());
                QueryWrapper<Drugs> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("ID",prescriptionDetailed.getDrugsID());
                Drugs drugs=drugsMapper.selectOne(queryWrapper3);
                prescriptionVO.setDrugs_name(drugs.getDrugsName());
                prescriptionVO.setPrice(drugs.getDrugsPrice());
                QueryWrapper<User> queryWrapper4=new QueryWrapper<>();
                queryWrapper4.eq("ID",prescription.getUserID());
                User user=userMapper.selectOne(queryWrapper4);
                prescriptionVO.setRealName(user.getRealName());
                prescriptionVOList.add(prescriptionVO);
            }
        }
        return CommonResponse.createForSuccess(prescriptionVOList);
    }
}
